package com.unknown.wificontrol.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.unknown.wificontrol.R;
import com.unknown.wificontrol.util.Constant;
import com.unknown.wificontrol.util.Preference;
import com.unknown.wificontrol.widget.CustomDialog;

import butterknife.ButterKnife;

/**
 * Created by jun on 2017/7/10.
 */

public abstract class BaseFragment extends Fragment {

    public Activity mActivity;

    protected final String BUNDLE = "bundle";

    private View rootView;

    private CustomDialog progressDialog;
    private TextView tvprogressMsg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        return inflater.inflate(getLayout(), container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rootView = view;
        ButterKnife.bind(this, view);
        initDialog();
        initViews(view);
    }


    private void initDialog() {
        progressDialog = new CustomDialog(mActivity, R.layout.layout_loading_dialog);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        tvprogressMsg = findView(progressDialog, R.id.tv_message);
    }

    abstract public int getLayout();

    abstract public void initViews(View parent);


    /**
     * 界面跳转
     *
     * @param targetClass ：跳转的目标界面
     * @param bundle      :传入的参数
     */
    protected void changeView(Class targetClass, Bundle bundle) {
        Intent intent = new Intent(getActivity(), targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivity(intent);

    }

    protected void changeView(Class targetClass) {
        Intent intent = new Intent(getActivity(), targetClass);
        startActivity(intent);
    }

    /**
     * 界面跳转
     *
     * @param targetClass 跳转class
     * @param bundle      数据参数
     * @param requestCode
     */
    protected void changeViewForResult(Class targetClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getActivity(), targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivityForResult(intent, requestCode);

    }

    /**
     * 界面跳转
     *
     * @param targetClass ：跳转的目标界面
     * @param bundle      :传入的参数
     * @param isFinish    ：是否关闭当前的activity
     */
    protected void changeView(Class targetClass, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(getActivity(), targetClass);
        if (null != bundle) {
            intent.putExtra(BUNDLE, bundle);
        }
        startActivity(intent);
        if (isFinish) {
            getActivity().finish();
        }
    }

    public void showToast(String text) {
        Toast result = new Toast(mActivity);
        LayoutInflater inflate = LayoutInflater.from(mActivity);
        View v = inflate.inflate(R.layout.layout_toast, null);
        TextView tv = v.findViewById(R.id.tv_message);
        tv.setText(text);
        result.setView(v);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.setDuration(Toast.LENGTH_SHORT);
        result.show();
    }


    public void snack(View view, String text) {
        Snackbar.make(view, text, Snackbar.LENGTH_SHORT).show();
    }

    protected <T extends View> T findView(View v, int id) {
        return (T) v.findViewById(id);
    }

    public <T extends View> T findView(Dialog dlg, int id) {
        return (T) dlg.findViewById(id);
    }

    protected String getDomain() {
        String host = Preference.get(mActivity, Constant.TAG_IP, Constant.defaultIP);
        return host.startsWith("http://") ? host : String.format("http://%s", host);
    }

    protected void showProgress(String text) {
        if (mActivity.isFinishing())
            return;
        if (progressDialog != null) {
            tvprogressMsg.setText(text);
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected void showProgress() {
        if (mActivity.isFinishing())
            return;
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected void hideProgress() {
        if (mActivity.isFinishing())
            return;
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}
